package org.qufenqi.api.controller.front;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.qufenqi.api.common.CommonResponse;
import org.qufenqi.api.entity.Account;
import org.qufenqi.api.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;



@Controller
@RequestMapping("/account/")
//@CrossOrigin(origins = "http://192.168.1.110:8090", maxAge = 3600)
public class AccountController {

    @Autowired
    private AccountService accountService;

    @PostMapping("register")
    @ResponseBody
    public CommonResponse<Account> register(@RequestBody Account account)
    {
        return accountService.register(account);
    }

    @PostMapping("login")
    @ResponseBody
    public CommonResponse<Account> login(@RequestBody Account account){
        return accountService.login(account.getUsername(),account.getPassword());
    }

    @PostMapping("phoneMSG")
    @ResponseBody
    public CommonResponse<String> getMsg(@RequestBody String number)throws Exception{

        JSONObject jsonObject = JSON.parseObject(number);
        String trueNumber = jsonObject.getString("number");

        return accountService.getMsg(trueNumber);
    }

    @PostMapping("checkID")
    @ResponseBody
    public CommonResponse<Object> checkID(@RequestBody Account account){
        return accountService.checkID(account.getName(),account.getIdNumber());
    }

    @PostMapping("insertUserID")
    @ResponseBody
    public CommonResponse<Account> insertUserID(@RequestBody String account){

        JSONObject jsonObject = JSON.parseObject(account);
        String username = jsonObject.getString("username");
        String idnumber = jsonObject.getString("idnumber");

        return accountService.insertUserID(username,idnumber);
    }

    @PostMapping("checkStudentID")
    @ResponseBody
    public CommonResponse<String> checkStudentId(@RequestBody String info){

        JSONObject jsonObject = JSON.parseObject(info);
        String school = jsonObject.getString("school");
        String studentName =jsonObject.getString("studentname");
        String studentId = jsonObject.getString("studentid");

        return accountService.checkStudentId(school,studentName,studentId);
    }

    @PostMapping("insertStudentID")
    @ResponseBody
    public CommonResponse<String> insertStudentId(@RequestBody String info){
        JSONObject jsonObject = JSON.parseObject(info);
        String username = jsonObject.getString("username");
        String studentId = jsonObject.getString("studentid");

        return accountService.insertStudentId(username,studentId);
    }

    @PostMapping("changePSW")
    @ResponseBody
    public CommonResponse<String> changePSW(@RequestBody String info){
        JSONObject jsonObject = JSON.parseObject(info);
        String username = jsonObject.getString("username");
        String psw = jsonObject.getString("password");

        return accountService.changePSW(username,psw);
    }

    @PostMapping("getUserInfo")
    @ResponseBody
    public CommonResponse<Account> getUserInfo(@RequestBody String info){

        JSONObject jsonObject = JSON.parseObject(info);
        String username = jsonObject.getString("username");

        return accountService.getUserInfo(username);
    }

    @PostMapping("addMoney")
    @ResponseBody
    public CommonResponse<Account> addMoney(@RequestBody String info){
        JSONObject jsonObject = JSON.parseObject(info);
        String username = jsonObject.getString("username");
        double money = jsonObject.getDoubleValue("usermoney");

        return accountService.addMoney(username,money);
    }
}
